#!/usr/bin/perl
#Run ALL the tagger!

use File::Copy;
use strict;
use warnings;
use encoding 'utf8';
use utf8;
use feature 'unicode_strings';

open STDERR, '>run_taggers_errors_log.txt' or die $!;#LOG all errors to a file

my $tnt_parabin="/home/zulu9/ma-scripts/trunk/tagger-bins/tnt/tnt-para";
my $tnt_bin="/home/zulu9/ma-scripts/trunk/tagger-bins/tnt/tnt";
my $tnt_testsuites="/home/zulu9/ma-scripts/trunk/testsuites/tt";
my $tnt_testsuites_gold="/home/zulu9/ma-scripts/trunk/testsuites/tt/gold";

my $cc_trainbin="/home/zulu9/ma-scripts/trunk/tagger-bins/candc-1.00/bin/train_pos";
my $cc_testbin="/home/zulu9/ma-scripts/trunk/tagger-bins/candc-1.00//bin/pos";
my $cc_testsuites="/home/zulu9/ma-scripts/trunk/testsuites/cc";
my $cc_testsuites_gold="/home/zulu9/ma-scripts/trunk/testsuites/cc/gold";

for (my $n=0;$n<=9;$n++){
	#Train TNT
	#my $train_command="$tnt_parabin"." -o $tnt_testsuites/test$n.tt/suite$n "."$tnt_testsuites/test$n.tt/suite$n.tt";
	#print "TNT TRAIN $n:\t".$train_command."\n";
	#system($train_command);
	
	my $train_command_gold="$tnt_parabin"." -o $tnt_testsuites_gold/test$n.tt_gold/suite$n"."_gold "."$tnt_testsuites_gold/test$n.tt_gold/suite$n"."_gold.tt";
	print "TNT TRAIN GOLD $n:\t".$train_command_gold."\n\n";		
	print STDERR "\n\nTNT TRAIN GOLD $n:\t".$train_command_gold."\n\n";	
	system($train_command_gold);	
	
	#Train CC
	#Hack to fix whitespace issues:
	print "Fixing: $cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold.cc\n";
	my $file="$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold.cc";
	my $clean_command='sed "s/^[ \t]*//;s/[ \t]*$//" < "'.$file.'" > "'.$file.'.1"';
	#print "$clean_command\n";
	system($clean_command); 
	
	print "Fixing: $cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold.c\n";
	$file="$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold.c";
	$clean_command='sed "s/^[ \t]*//;s/[ \t]*$//" < "'.$file.'" > "'.$file.'.1"';
	system($clean_command); 
	
	$train_command_gold="$cc_trainbin"." --model $cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold "."--input $cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold.cc.1 --comment suite$n";
	print "CC TRAIN GOLD $n:\t".$train_command_gold."\n\n";	
	print STDERR "\n\nCC TRAIN GOLD $n:\t".$train_command_gold."\n\n";	
	system($train_command_gold);	
	
	#Hack to avoid errors from unknown word tags (FIXME)
	open IN, "<:utf8", "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/unknowns" or die $!;
	open IN2, "<:utf8", "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/number_unknowns" or die $!;
	open OUT, ">:utf8", "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/unknowns.1" or die $!;
	open OUT2, ">:utf8", "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/number_unknowns.1" or die $!;
	while (<IN>){
		if ($_ =~m/#.*/){
			print OUT $_;
		}
	}
	while (<IN2>){
		if ($_ =~m/#.*/){
			print OUT2 $_;
		}
	}
	print OUT "\ngeneric_name\n";
	print OUT2 "\ngeneric_number\n";
	print "Unknown word files emptied.\n";
	my $filetobemoved = "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/unknowns.1";
	my $newfile = "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/unknowns";
	move($filetobemoved, $newfile) or die "Slice $filetobemoved cannot be moved to $newfile";
	print "Moved tempfile $filetobemoved to $newfile\n";
	my $oldfile= "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/unknowns.1";
	if (unlink($oldfile)) {
		print "File $oldfile deleted successfully.\n";
	} 
	else {#This should not happen
    	print "File $oldfile was not deleted.\n";
	}
	
	
	$filetobemoved = "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/number_unknowns.1";
	$newfile = "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/number_unknowns";
	move($filetobemoved, $newfile) or die "Slice $filetobemoved cannot be moved to $newfile";
	print "Moved tempfile $filetobemoved to $newfile\n";
	$oldfile= "$cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/number_ unknowns.1";
	if (unlink($oldfile)) {
		print "File $oldfile deleted successfully.\n";
	} 
	else {#This should not happen
    	print "File $oldfile was not deleted.\n";
	}
	#End hack

}


for (my $n=0;$n<=9;$n++){
	#Test TNT
	#my $test_command="$tnt_bin $tnt_testsuites/test$n.tt/suite$n $tnt_testsuites/test$n.tt/suite$n.t > $tnt_testsuites/test$n.tt/tnt_testout$n";
	#print "TNT TEST $n:\t".$test_command."\n";
	#system($test_command);
	
	my $test_command_gold="$tnt_bin $tnt_testsuites_gold/test$n.tt_gold/suite$n"."_gold $tnt_testsuites_gold/test$n.tt_gold/suite$n"."_gold.t > $tnt_testsuites_gold/test$n.tt_gold/tnt_testout_gold$n";
	print "TNT TEST GOLD $n:\t".$test_command_gold."\n\n";	
	print STDERR "\n\nTNT TEST GOLD $n:\t".$test_command_gold."\n\n";	
	system($test_command_gold);
	
	#Test CC  FIXME
	$test_command_gold="$cc_testbin"." --model $cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold --input $cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold.c.1 --output $cc_testsuites_gold/test$n.cc_gold/suite$n"."_gold/cc_output";
	print "CC TEST GOLD $n:\t".$test_command_gold."\n\n";	
	print STDERR "\n\nCC TEST GOLD $n:\t".$test_command_gold."\n\n";	
	system($test_command_gold);	
}

#TODO